package com.aster.common.api.service.imp;

import com.aster.common.api.dao.EscortUserInfoDao;
import com.aster.common.api.domain.TbEscortUserDuty;
import com.aster.common.api.domain.TbEscortUserInfo;
import com.aster.common.api.dto.DtoEscortUserDuty;
import com.aster.common.api.dto.DtoEscortUserInfo;
import com.aster.common.api.dto.dt2dto.EscortUserInfoConvert;
import com.aster.common.api.service.EscortUserDutyService;
import com.aster.common.api.service.EscortUserInfoService;
import com.aster.common.api.service.criteria.EscortUserDutyCriteria;
import com.aster.common.api.service.criteria.EscortUserInfoCriteria;
import com.aster.common.base.BaseServiceImpl;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.DateUtil;
import com.aster.common.utils.PageUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 
 * <br>
 * <b>功能：</b>TbEscortUserInfoService<br>
 */
@Service("escortUserInfoService")
public class  EscortUserInfoServiceImpl  extends BaseServiceImpl implements EscortUserInfoService {
  private final static Logger log= Logger.getLogger(EscortUserInfoServiceImpl.class);


	@Autowired
    private EscortUserInfoDao dao;
	@Autowired
    private EscortUserDutyService escortUserDutyService;

		
	public EscortUserInfoDao getDao() {
		return dao;
	}

	@Override
	public PageUtils getEscortUserInfoList(EscortUserInfoCriteria escortUserInfoCriteria) throws ServiceException {
		try{
			Integer count = super.queryByCount(escortUserInfoCriteria);
			if(count == null || count == 0){
				return new PageUtils(null,0);
			}
			List<TbEscortUserInfo> list = super.queryByList(escortUserInfoCriteria);
			List<DtoEscortUserInfo> dtoEscortUserInfoList = EscortUserInfoConvert.MAPPER.dos2dtos(list);
			setWorkingLife(dtoEscortUserInfoList);
			setDuty(dtoEscortUserInfoList,escortUserInfoCriteria.getTimeStart(),escortUserInfoCriteria.getTimeEnd());
			return new PageUtils(dtoEscortUserInfoList,count);
		}catch (Exception ex){
			ex.printStackTrace();
			throw new ServiceException(55555,ex.getMessage());
		}
	}

	@Override
	public TbEscortUserInfo getEscortByOpenId(String openId) {
		try{
			TbEscortUserInfo  tbEscortUserInfo= dao.getEscortByOpenId(openId);
			if(tbEscortUserInfo!=null){
				return tbEscortUserInfo;
			}
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return null;
	}


	private void setDuty(List<DtoEscortUserInfo> list, Date timeStart,Date timeEnd)throws ServiceException{
        EscortUserDutyCriteria criteria = new EscortUserDutyCriteria();
        Map<Integer,DtoEscortUserInfo> map = new HashMap<>();
        for (DtoEscortUserInfo dtoGeneralUserInfo:list){
            criteria.addEnterpriseIds(dtoGeneralUserInfo.getId());
            map.put(dtoGeneralUserInfo.getId(),dtoGeneralUserInfo);
        }
		if(timeStart == null){
			timeStart = DateUtil.nowZeroDateTime();
		}
		if(timeEnd == null){
            timeEnd = DateUtil.addDate(timeStart,7);
		}
        criteria.setTimeEnd(timeEnd);
        criteria.setTimeStart(timeStart);
		List<TbEscortUserDuty> dtoEscortUserDuties = escortUserDutyService.getListByIds(criteria);
		Map<Integer,List<DtoEscortUserDuty>> dutyMap = new HashMap<>();
		for (TbEscortUserDuty dtoEscortUserDuty:dtoEscortUserDuties){
			DtoEscortUserDuty dtoDuty = new DtoEscortUserDuty();
			dtoDuty.setDutyTime(String.valueOf(dtoEscortUserDuty.getDutyTime().getTime()));
			dtoDuty.setId(dtoEscortUserDuty.getId());
			dtoDuty.setDutyState(dtoEscortUserDuty.getDutyState());
			if(dutyMap.containsKey(dtoEscortUserDuty.getId())){
				List<DtoEscortUserDuty> dtoEscortUserDutyList = dutyMap.get(dtoEscortUserDuty.getId());
				dtoEscortUserDutyList.add(dtoDuty);
			}
			else{
				List<DtoEscortUserDuty> dtoEscortUserDutyList = new ArrayList<>();
				dtoEscortUserDutyList.add(dtoDuty);
				dutyMap.put(dtoEscortUserDuty.getId(),dtoEscortUserDutyList);
			}
		}
		if(dtoEscortUserDuties == null || dtoEscortUserDuties.size() == 0){
			for (DtoEscortUserInfo dtoEscortUserInfo:list){
				setEmptyDuty(dtoEscortUserInfo,timeStart,timeEnd);
			}
		}
		else{
			setDuty(map,dutyMap,timeStart,timeEnd);
		}
    }
	private void setWorkingLife(List<DtoEscortUserInfo> list){
		for (DtoEscortUserInfo dtoEscortUserInfo:list){
			Integer employmentDate = DateUtil.getDateYear(dtoEscortUserInfo.getEmploymentDate());
			Integer nowDate = DateUtil.getDateYear(DateUtil.nowZeroDateTime());
			Integer workingLife = employmentDate - nowDate;
			if(workingLife < 1 && workingLife > 0){
				dtoEscortUserInfo.setWorkData(1);
			}
			if(workingLife <= 0){
				dtoEscortUserInfo.setWorkData(0);
			}
			if(workingLife > 1){
				dtoEscortUserInfo.setWorkData(workingLife);
			}
		}
	}

	private void setDuty(Map<Integer,DtoEscortUserInfo> userInfoMap,Map<Integer,List<DtoEscortUserDuty>> dutyMap,Date timeStar,Date timeEnd){
		Iterator<Map.Entry<Integer,DtoEscortUserInfo>> entries = userInfoMap.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry<Integer,DtoEscortUserInfo> entry = entries.next();
			if(dutyMap.containsKey(entry.getKey())){
				fillEmptyDuty(entry.getValue(),dutyMap.get(entry.getKey()),timeStar,timeEnd);
			}
			else{
				setEmptyDuty(entry.getValue(),timeStar,timeEnd);
			}
		}
	}


	private void fillEmptyDuty(DtoEscortUserInfo dtoEscortUserInfo,List<DtoEscortUserDuty> list,Date timeStar,Date timeEnd){
		List<String> timeSpacing = DateUtil.getDatesBetweenDate(timeStar.toString(),timeEnd.toString());
		Map<String,String> map = new LinkedHashMap<>();
		for (String s:timeSpacing){
			map.put(s,s);
		}
		Map<String,DtoEscortUserDuty> dtoEscortUserDutyMap = new HashMap<>();
		for (DtoEscortUserDuty dtoEscortUserDuty:list){
			dtoEscortUserDutyMap.put(dtoEscortUserDuty.getDutyTime(),dtoEscortUserDuty);
		}
		List<DtoEscortUserDuty> dutyList = new ArrayList<>();
		Iterator<Map.Entry<String,String>> entries = map.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry<String,String> entry = entries.next();
			if(dtoEscortUserDutyMap.containsKey(entry.getKey())){
				dutyList.add(dtoEscortUserDutyMap.get(entry.getKey()));
			}else{
				DtoEscortUserDuty dtoEscortUserDuty = new DtoEscortUserDuty();
				dtoEscortUserDuty.setDutyState(0);
				String month = entry.getKey().substring(4,6);
				String day = entry.getKey().substring(6,8);
				dtoEscortUserDuty.setDutyTime(month.concat("-").concat(day));
			}
		}
		dtoEscortUserInfo.setEscortUserDutyList(dutyList);
	}

    private void setEmptyDuty(DtoEscortUserInfo dtoEscortUserInfo,Date timeStar,Date timeEnd){
		String star = DateUtil.dateToString(timeStar,"yyyyMMdd");
		String end = DateUtil.dateToString(timeEnd,"yyyyMMdd");
		List<String> timeSpacing = DateUtil.getDatesBetweenDate(star,end);
		List<DtoEscortUserDuty> list = new ArrayList<>();
		for (String time:timeSpacing){
			DtoEscortUserDuty dtoEscortUserDuty = new DtoEscortUserDuty();
			dtoEscortUserDuty.setDutyState(0);
			String month = time.substring(4,6);
			String day = time.substring(6,8);
			dtoEscortUserDuty.setDutyTime(month.concat("-").concat(day));
			list.add(dtoEscortUserDuty);
		}
		dtoEscortUserInfo.setEscortUserDutyList(list);
	}


}
